package com.bwie.system.controller;

import com.bwie.common.domain.TUser;
import com.bwie.common.domain.dto.ForgetPasswordDto;
import com.bwie.common.domain.dto.UserInfoReq;
import com.bwie.common.domain.dto.UserRegisteredReq;
import com.bwie.common.result.Result;
import com.bwie.system.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zt181
 * @date 2025/7/15
 */

@RestController
@RequestMapping("/system")
public class UserInfoController {

    @Autowired
    private TUserService userService;

    /**
     * 获取用户信息
     *
     * @param req 请求参数
     */
    @PostMapping("/getUserInfo")
    public Result<TUser> getUserInfo(@RequestBody UserInfoReq req) {
        TUser user = userService.getUserInfo(req);
        return Result.success(user);
    }

    @PostMapping("/save")
    public Result save(@RequestBody UserRegisteredReq req) {
        TUser user = new TUser();
        user.setEmail(req.getEmail());
        user.setMobile(req.getMobile());
        user.setPassword(req.getPassword());
        user.setNickname(req.getNickName());

        return userService.save(user) ? Result.success("注册成功") : Result.error("注册失败");
    }

    @PostMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody ForgetPasswordDto req) {
        TUser user = new TUser();
        user.setMobile(req.getMobile());
        user.setPassword(req.getPassword());

        return userService.forgetPassword(user) ? Result.success("修改密码成功") : Result.error("修改密码失败");
    }
}
